################################################################################
# create the environment

env = Environment()
env.Import(['TARGET', 'DEBUG', 'ECCFLAGS', 'LIBS', 'LIBPATH'])

INSTALLPATH = ARGUMENTS.get('InstallDir', '/opt/local/share/fluxus-018/plugins')
PROFILE = int(ARGUMENTS.get('profile', 0))

SOURCES = ['CITemplate.cpp', 'CIObjC.m', 'Surface.cpp']
STATIC = int(ARGUMENTS.get('STATIC', '0'))

if 'objc' not in LIBS:
	LIBS += ['objc']
FRAMEWORKS = ['AppKit', 'QuartzCore']

# to be able to build multiple variants from the same source
LIBSOURCES = ['FFGLPlugin.cpp']
LIBINCLUDES = ['./']	# add current directory to the include path

SOURCES += LIBSOURCES
INCLUDES = LIBINCLUDES

platform = env['PLATFORM']

CCFLAGS = '-Wall -Wno-unknown-pragmas -Wno-long-long ' + \
		'-pedantic -arch i386 '
CFLAGS = '-std=gnu99 -Wno-import '

LINKFLAGS = '-arch i386 '

if GetOption('bundle'):
	STATIC = 1
	LINKFLAGS += '-bundle '

if DEBUG:
	CCFLAGS += '-ggdb2 -O0 -DDEBUG=1 '
else:
	CCFLAGS += '-g0 -O3 '

env.Append(FRAMEWORKS = ['GLUT', 'OpenGL'])
env.Append(FRAMEWORKS = FRAMEWORKS)

if STATIC:
	if 'GLEW' in LIBS:
		LIBS.remove('GLEW')
		LIBS.append((File('/opt/local/lib/libGLEW.a')))

env.Append(CFLAGS = CFLAGS)
env.Append(CCFLAGS = CCFLAGS)
env.Append(CCFLAGS = ECCFLAGS)
env.Append(CPPPATH = INCLUDES)
env.Append(LINKFLAGS = LINKFLAGS)
env.Append(LIBS = LIBS)
env.Append(LIBPATH = LIBPATH)

# build the plugin

if GetOption('bundle'):
	env.Program(source = SOURCES, target = TARGET)
else:
	env.SharedLibrary(source = SOURCES, target = TARGET, SHLIBPREFIX = '')

# install
if not GetOption('bundle'):
	TARGET = TARGET + '.dylib'

if not GetOption('bundle'):
	env.Install(INSTALLPATH, TARGET)
else:
	env.Install(INSTALLPATH, TARGET + '.bundle')

env.Alias('install', INSTALLPATH)

# packaging / installing
if GetOption('bundle'): 
	from scripts.osxbundle import *
	TOOL_BUNDLE(env)
	env.Replace(FRAMEWORKS = Split('OpenGL GLUT AppKit QuartzCore'))
	env.Alias('bundle', env.MakeBundle(TARGET + '.bundle',
		TARGET,
		'key',
		'ffglcifilters.plist',
		typecode = 'BNDL'))

